import re, math
from collections import Counter
from nltk.corpus import stopwords




class Scorer:
    """identify the correlation between two texts""" 
    def __init__(self):
        pass


    def scorer(self, testText, originalText):
        """returns cosine similarity score between original text and the test text"""

        pattern = re.compile(r'[a-zA-Z0-9]+') 
        originalListOfWords1 = pattern.findall(originalText.lower())
        stop = stopwords.words('english') 
        originalListOfWords = [i for i in  originalListOfWords1 if i not in stop]
        #print "originalListOfWords: ",originalListOfWords,"\noriginalListOfWords1",originalListOfWords1
        dict1 = Counter(originalListOfWords)  
        testListOfWords1 = pattern.findall(testText.lower()) 
        testListOfWords = [i for i in  testListOfWords1 if i not in stop]
        dict2 = Counter(testListOfWords)
        return self.__cosineSimilarity(dict1,dict2)

    def __cosineSimilarity(self, dict1 , dict2):
        """return cosine similarity"""

        intersection = set(dict1.keys()).intersection(set(dict2.keys()))
        numerator = sum([dict1[x] * dict2[x] for x in intersection])
        norm_square_document1 = sum([dict1[x]**2 for x in dict1.keys()])
        norm_square_document2 = sum([dict2[x]**2 for x in dict2.keys()])
        denominator = math.sqrt(norm_square_document1) * math.sqrt(norm_square_document2)
        if  denominator != 0:
            return float(numerator) / denominator
        else:
            return 0

if __name__=="__main__":
    pass
